#!/usr/local/bin/python3
__author__ = '曹植'

from log import logger
import requests


def check_account_function(yyd_channel_sql, uid):
    """
    {'code': '1001', 'msg': '该用户为新用户', 'act': 'ajax', 'act_2': 'check_yhwx_user'}
    {'code': '1000', 'msg': '该用户已存在', 'act': 'ajax', 'act_2': 'check_yhwx_user'}
    测试:'8563696b7054f9010215bc855000e299'
    """
    try:
        mobile_md5 = yyd_channel_sql.query_mobile_md5_by_uid(uid)
        usable_channels = yyd_channel_sql.query_usable_channel_api()
        if usable_channels is None or mobile_md5 is None or uid is None:
            return
        for channel in usable_channels:
            channel_id = channel[0]
            channel_name = channel[1]
            storehouse_link = channel[2]
            hit_info = yyd_channel_sql.query_storehouse(uid, channel_id)
            if hit_info:
                continue
            form_data = {
                'mobile_md5': mobile_md5,
                'channel': '优逸贷'
            }
            response = requests.post(storehouse_link, json=form_data)
            result = response.json()
            if int(result['code']) == 1000:
                yyd_channel_sql.update_storehouse(uid, channel_id, channel_name)
            elif int(result['code']) == 1001:
                pass
            else:
                logger.error('优逸贷渠道api撞库接口返回异常:{}'.format(result))
    except:
        logger.exception('优逸贷渠道api撞库出错')
    finally:
        yyd_channel_sql.close()
